import { reqgetnew } from "../../http/api";
import { reqgetindexgoods } from "../../http/api";

/*初始值 */
const initState = {
    newshop: [],
    goodslist: [],
    n: 0,
}

/* 改名字 */
const types = {
    /* 修改newshop */
    CHANGE_NEWSHOP:"HOME_CHANGE_NEWSHOP",
    CHANGE_GOODSLIST:"HOME_CHANGE_GOODSLIST",
    CHNAGE_INDEX: "HOME_CHANGE_INDEX"
}


/* 改state */
const reducer = (state = initState, action) => {
    switch (action.type) {
        case types.CHANGE_NEWSHOP:
            return {
                ...state,
                newshop:action.newshop
            }
        case types.CHANGE_GOODSLIST:
            return{
                ...state,
                goodslist:action.goodslist
            }
        case types.CHNAGE_INDEX:
            return{
                ...state,
                n:action.n
            }
        default:
            return state;
    }
}


/* 做逻辑 */
export const actions = {
    changenewshop: newshop => ({type:types.CHANGE_NEWSHOP,newshop}),
    reqgetnew:()=>(dispatch,getStart)=>{
        reqgetnew().then(res=>{
            if(res.data.code === 200){
                dispatch(actions.changenewshop(res.data.list))
            }
        })
    },

    changegoodslist: goodslist => ({type:types.CHANGE_GOODSLIST,goodslist}),
    reqgetindexgoods: ()=>(dispatch,getStart)=>{
        reqgetindexgoods().then(res=>{
            if(res.data.code === 200){
                dispatch(actions.changegoodslist(res.data.list))
            }
        })
    },
    changeindex:n => ({type:types.CHNAGE_INDEX,n}),

}

/* 导出 */
/* 全部导出 */
// 数据
export const getNewshop = state => state.Home.newshop;
export const getGoodslist = state => state.Home.goodslist;
export const changeIndex = state => state.Home.n;

export default reducer;